package pile.customer.service.impl;

import pile.Customer;
import pile.customer.config.CustomerServiceWebSocketServerUtil;
import pile.customer.entity.CustomerService;
import pile.customer.mapper.CustomerServiceMapper;
import pile.customer.params.customerService.LoginDTO;
import pile.customer.service.ICustomerServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pile.customer.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import pile.customer.utils.JwtUtils;
import pile.customer.utils.WebUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pile.customer.entity.PageList;
import org.apache.commons.lang.StringUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import pile.customer.params.customerService.DeleDTO;
import pile.customer.params.customerService.ListDTO;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭天义
 * @since 2024-06-01
 */
@Service
public class CustomerServiceServiceImpl extends ServiceImpl<CustomerServiceMapper, CustomerService> implements ICustomerServiceService {

    @Autowired
    private CustomerServiceMapper customerServiceMapper;

    @Autowired
    private CustomerServiceWebSocketServerUtil customerServiceWebSocketServerUtil;


    /**
     * 根据用户名  密码登录
     * @param loginDTO
     * @return
     */
    @Override
    public ResponseResult passwordLogin(LoginDTO loginDTO) {
        if(loginDTO.getAccountNumber()==null){
            return new ResponseResult(203,"用户名不能为空");
        }
        if(loginDTO.getPassword()==null){
            return new ResponseResult(203,"密码不能为空");
        }

        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            //转成字节数组
            byte[] passwordByte = md.digest(loginDTO.getPassword().getBytes());
            //在在转换成16位字符串密码
            String encodePassword = new BigInteger(1, passwordByte).toString(16);

            //根据账户查询数据库密码
            CustomerService customerService = customerServiceMapper.selectOne(new QueryWrapper<CustomerService>().eq("account_number", loginDTO.getAccountNumber()));
            if(customerService==null){
                return new ResponseResult(203,"用户名不存在");
            }

            //判断两个密码是否相等
            if( ! encodePassword.equals(customerService.getPassword())){
                //不相等 直接返回.
                return new ResponseResult(203,"密码错误");
            }
           /* //查询账户是否已在线
            boolean onLine = customerServiceWebSocketServerUtil.isOnLine(customerService.getAccountNumber());

            if(onLine){
                //在线的话就把他挤掉
                customerServiceWebSocketServerUtil.offline(customerService.getAccountNumber());
            }*/
            //相等   返回token
            CustomerService customerServiceToken = new CustomerService();

            String jwtToken = JwtUtils.getJwtToken(customerServiceToken);

            Map map = new HashMap();
            map.put("token",jwtToken);
            map.put("nickName",customerService.getNickName());
            map.put("accountNumber",customerService.getAccountNumber());

            //登录成功清空
            customerServiceWebSocketServerUtil.offline(customerService.getAccountNumber());
            return new ResponseResult(200,map);
        }catch (Exception e){
            log.error("登录异常，请稍后再试。"+e);
            return new ResponseResult(203,"录异常，请稍后再试。");
        }

    }
}
